package easytodo.client;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import easytodo.client.ArrayAdapters.NoteArrayAdapter;
import easytodo.client.data.NoteHandler;
import easytodo.client.data.OnNoteChangedListener;
import easytodo.client.data.SQLiteNoteHandlerImpl;
import easytodo.model.Note;

public class EasyTodo extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		noteHandler = new SQLiteNoteHandlerImpl(this);
		noteHandler.setOnNoteChangedListener(noteChanged);
		theItems = new ArrayList<Note>();
		aa = new NoteArrayAdapter(this, R.layout.note_layout, theItems);
		theList = (ListView) findViewById(R.id.noteItemList);
		theList.setAdapter(aa);
		registerForContextMenu(theList);

		refreshItems();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(0, ADD_NEW_ITEM_MENU, Menu.NONE, R.string.addItemText);
		menu.add(0, REFRESH_ITEMS_MENU, Menu.NONE, R.string.refreshItemText);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);

		boolean handled = true;

		switch (item.getItemId()) {
		case (ADD_NEW_ITEM_MENU):
			addItem();
			break;
		case (REFRESH_ITEMS_MENU):
			refreshItems();
			break;
		default:
			handled = false;
		}

		return handled;
	}

	private void refreshItems() {
		theItems.clear();
		theItems.addAll(noteHandler.getAllNotes());
		aa.notifyDataSetChanged();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		if (v.getId() == R.id.noteItemList) {
			initializeNoteItemContextMenu(menu, menuInfo);
		}
	}

	private void initializeNoteItemContextMenu(ContextMenu menu,
			ContextMenuInfo menuInfo) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
		Note selectedItem = (Note) theList.getItemAtPosition(info.position);
		menu.setHeaderTitle(selectedItem.Title);
		menu.add(ContextMenu.NONE, REMOVE_ITEM_CMENU, ContextMenu.NONE,
				R.string.removeItemText);
		menu.add(ContextMenu.NONE, EDIT_ITEM_CMENU, ContextMenu.NONE,
				R.string.editItemText);
		menu.add(ContextMenu.NONE, SEND_ITEM_CMENU, ContextMenu.NONE,
				R.string.sendItemText);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		super.onContextItemSelected(item);

		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int selectedIndex = info.position;

		Note selectedNote = theItems.get(selectedIndex);

		switch (item.getItemId()) {
		case (REMOVE_ITEM_CMENU):
			removeItem(selectedNote);
			break;
		case (EDIT_ITEM_CMENU):
			editItem(selectedNote);
			break;
		case (SEND_ITEM_CMENU):
			sendItem(selectedNote);
			break;
		}

		return false;
	}

	private void sendItem(Note selectedNote) {

	}

	private void editItem(Note selectedNote) {
		Intent i = new Intent(this, EditNote.class);
		i.putExtra("easytodo.model.Note", selectedNote);
		startActivityForResult(i, EDIT_ITEM);
	}

	private OnNoteChangedListener noteChanged = new OnNoteChangedListener() {
		public void onNotesChanged() {
			refreshItems();
		}

		public void onNoteChanged(long id, Boolean isRemoved) {
			for (Note note : theItems) {
				if (note.ID == id) {
					theItems.remove(note);
					break;
				}
			}
			if (isRemoved == false) {
				Note note = noteHandler.getNote(id);
				if (note != null)
					theItems.add(note);
			}
			aa.notifyDataSetChanged();
		}
	};

	private void addItem() {
		Intent i = new Intent(this, EditNote.class);
		startActivityForResult(i, ADD_ITEM);
	}

	private void removeItem(Note selectedNote) {
		noteHandler.removeNote(selectedNote.ID);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == Activity.RESULT_OK) {
			Note note = (Note) data.getParcelableExtra("easytodo.model.Note");
			switch (requestCode) {
			case (ADD_ITEM):
				noteHandler.saveNote(note);
				break;
			case (EDIT_ITEM):
				noteHandler.editNote(note);
				break;
			}
		}
	}

	private ArrayList<Note> theItems;
	private ListView theList;
	private NoteArrayAdapter aa;
	private NoteHandler noteHandler;

	// Intent codes
	static final private int ADD_ITEM = 123456;
	static final private int EDIT_ITEM = 234567;

	// Menu item indexes.
	static final private int ADD_NEW_ITEM_MENU = Menu.FIRST;
	static final private int REFRESH_ITEMS_MENU = Menu.FIRST + 1;

	// Context menu indexes.
	static final private int REMOVE_ITEM_CMENU = ContextMenu.FIRST;
	static final private int EDIT_ITEM_CMENU = ContextMenu.FIRST + 1;
	static final private int SEND_ITEM_CMENU = ContextMenu.FIRST + 2;
}